{% extends 'base.html' %}

{% block title %}信息列表{% endblock %}

{% block content %}
<div class="d-flex justify-content-between mb-3">
    <h4>信息列表</h4>
    <a href="/new_info" class="btn btn-primary">新建信息</a>
</div>

<table class="table table-hover">
    <thead class="table-light">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>平台</th>
            <th>风格</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        {% for item in pager.items %}
        <tr>
            <td>{{ item.id }}</td>
            <td>{{ item.title }}</td>
            <td>{{ item.platform.name if item.platform else '' }}</td>
            <td>{{ item.style.name if item.style else '' }}</td>
            <td>{{ item.status.name if item.status else '' }}</td>
            <td>
                <a href="#" class="btn btn-sm btn-outline-secondary">编辑</a>
                <a href="#" class="btn btn-sm btn-outline-danger" onclick="confirmDelete({{ item.id }})">删除</a>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

<!-- 分页导航 -->
<nav aria-label="Page navigation">
    <ul class="pagination justify-content-center">
        <li class="page-item {% if not pager.has_prev %}disabled{% endif %}">
            <a class="page-link" href="?page={{ pager.prev_num }}">上一页</a>
        </li>
        {% for page_num in pager.iter_pages() %}
        <li class="page-item {% if page_num == pager.page %}active{% endif %}">
            <a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a>
        </li>
        {% endfor %}
        <li class="page-item {% if not pager.has_next %}disabled{% endif %}">
            <a class="page-link" href="?page={{ pager.next_num }}">下一页</a>
        </li>
    </ul>
</nav>

<script>
    function confirmDelete1(id) {
        if (confirm('确定要删除该信息吗？')) {
            window.location.href = '/delete_info/' + id;
        }
    }
    // 确认删除函数
    async function confirmDelete(id) {
        if (confirm('确定要删除吗？')) {
            const response = await fetch(`/`, {
                method: 'DELETE',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: new URLSearchParams({id})
            });
            
            if (response.ok) {
                location.reload();
            }
        }
    }
</script>
{% endblock %}